package unit2.csvPaser;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.List;

import unit2.question.EnterAnswerQuestion;

import com.googlecode.jcsv.reader.CSVEntryParser;
import com.googlecode.jcsv.reader.CSVReader;
import com.googlecode.jcsv.reader.internal.CSVReaderBuilder;

public class EnterAnswerQuestionEntryParser implements CSVEntryParser<EnterAnswerQuestion> {
	  @Override
	  public EnterAnswerQuestion parseEntry(String... data) {
	    if (data.length != 2) {
	      throw new IllegalArgumentException("data is not a valid person record");
	    }	                
	    String question = data[0];
	    String answer = data[1];
	    return  new EnterAnswerQuestion (question, answer);
	  }
	  public List<EnterAnswerQuestion> readQuestions(String adressOfFile){
			Reader reader = null;
			try {
				reader = new FileReader(adressOfFile);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			CSVReader<EnterAnswerQuestion> csvQuestionReader = new CSVReaderBuilder<EnterAnswerQuestion>(reader).entryParser(new EnterAnswerQuestionEntryParser()).build();
			List<EnterAnswerQuestion> questions = null;
			try {
				questions = csvQuestionReader.readAll();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return questions;
		
		}
	}

